// pages/picker/picker.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
    multiArray: [{
        id: 1,
        label: "四川省",
        children: [{
            id: 2,
            label: "绵阳市",
            children: [{
                id: 3,
                label: "仙台县",
              },
              {
                id: 4,
                label: "梓潼县",
              },
            ],
          },
          {
            id: 7,
            label: "成都市",
            children: [{
                id: 8,
                label: "天府新区",
              },
              {
                id: 9,
                label: "武侯区",
              },
              {
                id: 10,
                label: "双流区",
              },
            ],
          },
          {
            id: 12,
            label: "广元市",
            children: [{
                id: 13,
                label: "苍溪县",
              },
              {
                id: 14,
                label: "旺苍县",
              },
              {
                id: 15,
                label: "剑阁",
              },
            ],
          },
        ],
      },
      {
        id: 17,
        label: "黑龙江省",
        children: [{
            id: 18,
            label: "哈尔滨市",
            children: [{
                id: 19,
                label: "五常市",
              },
              {
                id: 20,
                label: "阿城区",
              },
            ],
          },
          {
            id: 21,
            label: "齐齐哈尔",
            children: [{
                id: 22,
                label: "龙沙区",
              },
              {
                id: 23,
                label: "建华区",
              },
            ],
          },
        ],
      },
    ],
    multiIndex: [0, 0, 0],
    multiIds: [],
    newArr: [],
    region:"",
    detailedAddress:"",
    consigneeName:"",
    phone:"",
    allAddress:"",
    addressArr:"",
    addressIndex:""
  },
 
  bindMultiPickerChange(e) {
    console.log(this.data.multiIds);
    let Arr=[]
    let str=""
    this.data.multiIds.forEach(item=>{
        str+=item.label
        Arr.push(item.label)
    })
    this.setData({
      region:str,
      addressArr:Arr
    })
  },
  bindMultiPickerColumnChange(e) {
    console.log(1);
    let data = {
      newArr: this.data.newArr,
      multiIndex: this.data.multiIndex,
      multiIds: this.data.multiIds,
    };
    data.multiIndex[e.detail.column] = e.detail.value;
 
    let searchColumn = () => {
      let arr1 = [];
      let arr2 = [];
      this.data.multiArray.map((v, vk) => {
        if (data.multiIndex[0] === vk) {
          data.multiIds[0] = {
            ...v,
          };
          v.children.map((c, ck) => {
            arr1.push(c.label);
            if (data.multiIndex[1] === ck) {
              data.multiIds[1] = {
                ...c,
              };
              c.children.map((t, vt) => {
                arr2.push(t.label);
                if (data.multiIndex[2] === vt) {
                  data.multiIds[2] = {
                    ...t,
                  };
                }
              });
            }
          });
        }
      });
      data.newArr[1] = arr1;
      data.newArr[2] = arr2;
    };
    switch (e.detail.column) {
      case 0:
        // 每次切换还原初始值
        data.multiIndex[1] = 0;
        data.multiIndex[2] = 0;
        // 执行函数处理
        searchColumn();
        break;
      case 1:
        data.multiIndex[2] = 0;
        searchColumn();
        break;
      case 2:
        searchColumn();
        break;
 
    }
    this.setData(data);
    console.log(this.data);
  },
  detailedAddressInput(e){
    this.setData({
      detailedAddress:e.detail.value
    })
  },
  consigneeNameInput(e){
    this.setData({
      consigneeName:e.detail.value
    })
  },
  phoneInput(e){
    this.setData({
      phone:e.detail.value
    })
  },
 
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // console.log(options.index);
    this.setData({
      addressIndex:options.index
    })
    this.getInfo(this.data.addressIndex)
    let state = {
      arr: [],
      arr1: [],
      arr2: [],
      arr3: [],
      multiIds: []
    }
    this.data.multiArray.map((v, vk) => {
      state.arr1.push(v.label);
      if (this.data.multiIndex[0] === vk) {
        state.multiIds[0] = v;
      }
      if (state.arr2.length <= 0) {
        v.children.map((c, ck) => {
          state.arr2.push(c.label);
          if (this.data.multiIndex[1] === ck) {
            state.multiIds[1] = c;
          }
          if (state.arr3.length <= 0) {
            c.children.map((t, tk) => {
              state.arr3.push(t.label);
              if (this.data.multiIndex[2] === tk) {
                state.multiIds[2] = t;
              }
            });
          }
        });
      }
    });
    state.arr[0] = state.arr1;
    state.arr[1] = state.arr2;
    state.arr[2] = state.arr3;
    this.setData({
      newArr: state.arr,
      multiIds: state.multiIds,
    });
  },
  // 获取页面的数据
  getInfo(index){
    let addressList=wx.getStorageSync('address')
    let addressInfo=addressList[index]
    this.setData({
      region:addressInfo.provinceName+addressInfo.cityName+addressInfo.countyName,
      detailedAddress:addressInfo.detailInfo,
      consigneeName:addressInfo.userName,
      phone:addressInfo.telNumber,
      allAddress:addressInfo,
      addressArr:[addressInfo.provinceName,addressInfo.cityName,addressInfo.countyName]
    })
  },
// 保存
  getsava(){
    let Obj=this.data.allAddress
    Obj.userName=this.data.consigneeName
    Obj.telNumber=this.data.phone
    Obj.provinceName=this.data.addressArr[0]
    Obj.cityName=this.data.addressArr[1]
    Obj.countyName=this.data.addressArr[2]
    Obj.detailInfo=this.data.detailedAddress
    let StrongAddress=wx.getStorageSync('address')
    StrongAddress.splice(this.data.addressIndex,1,Obj)
    wx.setStorageSync('address', StrongAddress)
    console.log(Obj);
    wx.navigateBack()
  }
});